package com.renren.webmaven2.service;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.renren.eliya.domain.BigClass;
import com.renren.eliya.domain.SmallClass;
import com.renren.webmaven2.util.CacheUtils;
import com.renren.webmaven2.util.Constants;
import com.renren.yx.base.dao.daoutil.InitDao;

/**
 * write
 * 
 * @author jizhong.yang
 * @version V1.0
 */

@Service
@SuppressWarnings("unchecked")
public class SmallClassService {

	@Autowired
	private MonitorService monitorService;

	public int add(String name, String weight, int bigClassId,
			Integer isIndexView, String small_class_content) {
		try {
			SmallClass smallClass = new SmallClass();
			smallClass.setName(name);
			smallClass.setWeight(Integer.valueOf(weight));
			smallClass.setCreateTime(new Timestamp(System.currentTimeMillis()));
			smallClass.setBigClassId(bigClassId);
			smallClass.setIsIndexView(isIndexView);
			smallClass.setContent(small_class_content);
			BigClass bigClass = (BigClass) InitDao.getDao().getEntity(
					BigClass.class, bigClassId);
			smallClass.setBigClassName(bigClass.getName());
			InitDao.getDao().addEntity(smallClass);
			monitorService.getSmallClasses();
			return 1;
		} catch (Exception e) {
			return 0;
		}
	}

	public int del(Integer id) {
		try {
			SmallClass smallClass = (SmallClass) InitDao.getDao().getEntity(
					SmallClass.class, id);
			InitDao.getDao().delEntity(smallClass);
			monitorService.getSmallClasses();
			return 1;
		} catch (Exception e) {
			return 0;
		}
	}

	public List<SmallClass> findByBigClass(Integer bigClassId) {
		List<SmallClass> smallClasses = InitDao.getDao().findEntities(
				"from SmallClass s where s.bigClassId=" + bigClassId
						+ " order by s.weight ");
		return smallClasses;
	}

	public int indexView(Integer id) {
		try {
			SmallClass smallClass = (SmallClass) InitDao.getDao().getEntity(
					SmallClass.class, id);
			if (smallClass.getIsIndexView() == 0) {
				smallClass.setIsIndexView(1);
			} else {
				smallClass.setIsIndexView(0);
			}
			InitDao.getDao().updateEntity(smallClass);
			monitorService.getSmallClasses();
			return 1;
		} catch (Exception e) {
			return 0;
		}
	}

	public List<SmallClass> findByIndexView(int indexView) {
		List<SmallClass> smallClasses = (List<SmallClass>) CacheUtils
				.get(Constants.NET_CACHE_ALL_SMALLCLASS);
		List<SmallClass> smallClassesForIndexView = new ArrayList<SmallClass>();
		for (SmallClass smallClass : smallClasses) {
			if (smallClass.getIsIndexView() == indexView) {
				smallClassesForIndexView.add(smallClass);
			}

		}
		return smallClassesForIndexView;
	}

	public List<SmallClass> selectSmallClass(Integer big_class_id) {
		List<SmallClass> smallClasses = (List<SmallClass>) CacheUtils
				.get(Constants.NET_CACHE_ALL_SMALLCLASS);
		List<SmallClass> smallClassesForBigclass = new ArrayList<SmallClass>();
		for (SmallClass smallClass : smallClasses) {
			if (smallClass.getBigClassId() == big_class_id) {
				smallClassesForBigclass.add(smallClass);
			}

		}
		return smallClassesForBigclass;
	}

	public SmallClass get(Integer smallClassId) {
		List<SmallClass> smallClasses = (List<SmallClass>) CacheUtils
				.get(Constants.NET_CACHE_ALL_SMALLCLASS);
		for (SmallClass smallClass : smallClasses) {
			if (smallClass.getId() == smallClassId) {
				return smallClass;
			}

		}
		return null;
	}

	public int update(String smallClassName, Integer weight, Integer id) {
		try {
			SmallClass smallClass = (SmallClass) InitDao.getDao().getEntity(
					SmallClass.class, id);
			smallClass.setName(smallClassName);
			smallClass.setWeight(weight);
			InitDao.getDao().updateEntity(smallClass);
			monitorService.getSmallClasses();
			return 1;
		} catch (Exception e) {
			return 0;
		}
	}

}
